package com.yeung.swordoffer;

import java.util.Stack;

/**
 * ["PSH1","PSH2","PSH3","POP","POP","PSH4","POP","PSH5","POP","POP"]
 * <p>
 * 1,2,3,4,5
 */
public class 两个栈实现队列 {

    static Stack<Integer> input = new Stack<Integer>();
    static Stack<Integer> output = new Stack<Integer>();

    public static void main(String[] args) {
        String arr[] = {"PSH1", "PSH2", "PSH3", "POP", "POP", "PSH4", "POP", "PSH5", "POP", "POP"};
        for (String s : arr) {
            if (s.equals("POP")) {
                //弹出元素
                int res = pop();
                System.out.println(res);
            } else {
                String sub = s.substring(3);
                push(Integer.valueOf(sub));
            }
        }
    }

    public static void push(int node) {
        //将output中的数据全部push进来(反转顺序了)
        input.clear();
        while (!output.empty()) {
            input.push(output.pop());
        }
        //将新来的元素装进input-> 即进队
        input.push(node);
        //更新出队的序列
        // 将input中的数据全部push进来(又一次反转顺序了)
        output.clear();
        while (!input.isEmpty())
            output.push(input.pop());
    }

    public static int pop() {
        //出队
        if (!output.isEmpty())
            return output.pop();
        else
            return -1;
    }

}
